﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ProductManager_AddProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            GetPingPai();
            RECORDID.Text = "";
            String rID = Page.Request["ID"];

            if (rID != null && rID != "")
            {
                //修改已有产品
                EntityOprHelper helper = new EntityOprHelper();
                T_PRODUCT _product = helper.GetEntityByWhere<T_PRODUCT>(" where ID="+rID+"");

                shareCode _share=new shareCode();
                string bigType = _share.getBigTypeBySmallType(_product.PRODUCT_SMALLTYPEID);
                string type = _share.getTypeByBigType(bigType);

                getLeiXing();
                pZhanShiQuYu.SelectedValue = type;

                getDaLei(type);
                pChanPingDaLei.SelectedValue = bigType;

                getXiaoLei(bigType);
                pChanPingXiaLei.SelectedValue = _product.PRODUCT_SMALLTYPEID;

                pPingPai.SelectedValue = _product.BRANDID;
                pXiaoshouLeiXing.SelectedValue = _product.SALETYPE;

                pKuCunShuLiang.Text = _product.STOCK_SUM.ToString("N");
                pZhongLiang.Text = _product.WEIGHT.ToString("N");
                pShiChangJiaGe.Text = _product.MARKETPRICE.ToString("N");
                pJiaGe.Text = _product.PRICE.ToString("N");
                pZheKouLu.SelectedValue = _product.DISCOUNT.ToString();
                pTuPian.Text = _product.IMAGE;
                pMiaoShu.Text = _product.DESCRIPTION;

                pNAME.Text = _product.NAME;
                RECORDID.Text = _product.ID.ToString();
            }
            else
            { 
                //新添加产品
                getLeiXing();
                getDaLei(pZhanShiQuYu.SelectedValue);
                getXiaoLei(pChanPingXiaLei.SelectedValue);
            }
        }
    }

    //得到品牌类型
    private void GetPingPai()
    {
        EntityOprHelper helper = new EntityOprHelper();
        DataTable _table = helper.GetDataTableBySql("select BARNDID,NAME FROM T_BARND");

        pPingPai.DataValueField = "BARNDID";
        pPingPai.DataTextField = "NAME";

        pPingPai.DataSource = _table.DefaultView;
        pPingPai.DataBind();
    
    }
    //得到展示区域
    private void getLeiXing()
    { 
                EntityOprHelper helper = new EntityOprHelper();
                DataTable _table = helper.GetDataTableBySql("select TYPEID,NAME FROM T_TYPE");

                pZhanShiQuYu.DataValueField = "TYPEID";
                pZhanShiQuYu.DataTextField = "NAME";

                pZhanShiQuYu.DataSource = _table.DefaultView;
                pZhanShiQuYu.DataBind();

    }
    //得到大类
    private void getDaLei(String pid)
    {
        EntityOprHelper helper = new EntityOprHelper();
        DataTable _table = helper.GetDataTableBySql("select PID,NAME FROM T_PRODUCT_BIGTYPE WHERE TYPEID='" + pid + "'");

        pChanPingDaLei.DataValueField = "PID";
        pChanPingDaLei.DataTextField = "NAME";

        pChanPingDaLei.DataSource = _table.DefaultView;
        pChanPingDaLei.DataBind();
    }
    //得到小类
    private void getXiaoLei(String pid)
    { 
        EntityOprHelper helper = new EntityOprHelper();
        DataTable _table = helper.GetDataTableBySql("select PID,NAME FROM T_PRODUCT_SMALLTYPE WHERE PRODUCT_BIGTYPEID='" + pid + "'");

        pChanPingXiaLei.DataValueField = "PID";
        pChanPingXiaLei.DataTextField = "NAME";

        pChanPingXiaLei.DataSource = _table.DefaultView;
        pChanPingXiaLei.DataBind();
    
    
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        String userNames = pNAME.Text;
        if (userNames == "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('请输入产品名称')", true);
            return;
        }
        EntityOprHelper helper = new EntityOprHelper();

        if (RECORDID.Text != "")
        {
            //System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
            //provider.PercentDecimalDigits = 2;

            T_PRODUCT _address1 = new T_PRODUCT();
            _address1.NAME = userNames;
           // _address.PID = System.Guid.NewGuid().ToString("N").ToUpper();
            _address1.IMAGE = pTuPian.Text;
            _address1.PRICE = Convert.ToDouble(pJiaGe.Text);
            _address1.PRODUCT_SMALLTYPEID = pChanPingXiaLei.SelectedValue;
            _address1.SALETYPE = pXiaoshouLeiXing.SelectedValue;
            _address1.STOCK_SUM = Convert.ToDouble(pKuCunShuLiang.Text);
            _address1.WEIGHT = Convert.ToDouble(pZhongLiang.Text);
            _address1.DISCOUNT = Convert.ToDouble(pZheKouLu.Text);
            _address1.DESCRIPTION = pMiaoShu.Text;
            _address1.MARKETPRICE = Convert.ToDouble(pShiChangJiaGe.Text);
            _address1.BRANDID = pPingPai.SelectedValue;


            bool b1 = helper.EditEntity<T_PRODUCT>(_address1);
            if (b1)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改成功')", true);
                pNAME.Text = "";
                RECORDID.Text = "";

            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改失败')", true);
            }

            return;
        }




        T_PRODUCT oldUser = helper.GetEntityByWhere<T_PRODUCT>(" where name='" + userNames + "'");
        if (oldUser != null)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('产品名称已经存在!')", true);
            return;
        }

        T_PRODUCT _address = new T_PRODUCT();
        _address.NAME = userNames;
        _address.PID = System.Guid.NewGuid().ToString("N").ToUpper();
        _address.IMAGE = pTuPian.Text;
        _address.PRICE =Convert.ToDouble(pJiaGe.Text);
        _address.PRODUCT_SMALLTYPEID = pChanPingXiaLei.SelectedValue;
        _address.SALETYPE = pXiaoshouLeiXing.SelectedValue;
        _address.STOCK_SUM = Convert.ToDouble(pKuCunShuLiang.Text);
        _address.WEIGHT = Convert.ToDouble(pZhongLiang.Text);
        _address.DISCOUNT = Convert.ToDouble(pZheKouLu.Text);
        _address.DESCRIPTION = pMiaoShu.Text;
        _address.MARKETPRICE = Convert.ToDouble(pShiChangJiaGe.Text);
        _address.BRANDID = pPingPai.SelectedValue;


        bool b = helper.AddEntity<T_PRODUCT>(_address);
        if (b)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加成功')", true);
            pNAME.Text = "";
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加失败')", true);
        }


    }
    protected void pZhanShiQuYu_SelectedIndexChanged(object sender, EventArgs e)
    {
        getDaLei(pZhanShiQuYu.SelectedValue);
        getXiaoLei(pChanPingDaLei.SelectedValue);

    }
    protected void pChanPingDaLei_SelectedIndexChanged(object sender, EventArgs e)
    {
        getXiaoLei(pChanPingDaLei.SelectedValue);
    }
    protected void pChanPingXiaLei_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (pTuPian.Text == "")
            pTuPian.Text = shareCode.upFileToServer();
        else
            pTuPian.Text = pTuPian.Text + "," + shareCode.upFileToServer();
    }
}
